package com.aischool.webapp.family;

import com.aischool.common.model.AiLifePieces;
import com.aischool.routes.RouteMapping;
import com.aischool.service.AiLifePiceseService;
import com.aischool.utils.DateUtil;
import com.aischool.utils.JsonCodeEnum;
import com.aischool.utils.JsonResult;
import com.jfinal.aop.Inject;
import com.jfinal.kit.StrKit;

@RouteMapping(url = "/front/family/lifepieces")
public class LifePiecesController extends BaseFamilyController {
	
	@Inject
	AiLifePiceseService aiLifePiceseService;

	public void edit() {
		int type_id = getInt("typeid");
		String type_name = type_id == 1 ? "宝宝相册" : "生活动态";
		this.setAttr("attr_type_id", type_id);
		this.setAttr("attr_type_name", type_name);
		
		
		render(view_path + "/lifepieces_edit.html");
	}
	
	public void save() {
		JsonResult jr =new JsonResult();


		int login_user_id = this.getAttrForInt("login_user_id");
		int login_org_id = this.getAttrForInt("login_org_id");
		int login_class_id = this.getAttrForInt("login_class_id");
		int login_student_id = this.getAttrForInt("login_student_id");
		
		int picese_type_id = getInt("picese_type_id");
		
		AiLifePieces info = getModel(AiLifePieces.class, "", true);
		
		if (StrKit.isBlank(info.getPiceseInfo()) && StrKit.isBlank(info.getPiceseImgs())) {
			jr.appendMsg("请输入要发布的信息或者上传图片！");
		}else if (picese_type_id == 1 && StrKit.isBlank(info.getPiceseImgs())) {
			jr.appendMsg("请上传图片！");
		}else {
			boolean bf = false;
			
			info.setUpdateTime(DateUtil.getTimestamp());
			info.removeNullValueAttrs();
			
			if (info.getPiceseId() != null && info.getPiceseId()>0) {
				bf = info.update();
			}else {
				info.setAddUserid(login_user_id);
				info.setAddTime(DateUtil.getTimestamp());
				info.setOrgId(login_org_id);
				info.setClassId(login_class_id);
				info.setStudentId(login_student_id);
				info.setIsAlbum(false);
				info.setIsLife(false);
				if (StrKit.notBlank(info.getPiceseImgs())) {
					info.setIsAlbum(true);
				}
				if (picese_type_id == 2) {
					info.setIsLife(true);
				}
				info.setPiceseState(1);
				bf = info.save();
			}
			
			if (bf) {
				jr.setEnum(JsonCodeEnum.Success);
			}else {
				String msg = jr.getMsg();
				jr.setEnum(JsonCodeEnum.Faild);
				jr.appendMsg(msg);
			}
		}
		
		renderJson(jr.Dic);
	}
	
	public void del() {
		JsonResult jr =new JsonResult();
		int login_user_id = this.getAttrForInt("login_user_id");
		boolean bf = false;
		int id = getInt("id",0);
		if (id > 0) {
			bf = aiLifePiceseService.del(id, login_user_id);
		}
		if (bf) {
			jr.setEnum(JsonCodeEnum.Success);
		}else {
			jr.setEnum(JsonCodeEnum.Faild);
		}
		renderJson(jr.Dic);
	}
}
